﻿using System;
using Books.Model;
using RefLib.Data;

namespace Books.DAL.ModelProxies
{
    internal class PublisherProxy : PublisherModel
    {
        public PublisherProxy()
        {
            _authors = new Lazy<AuthorModel[]>(() =>
                {
                    using (var db = ContextBuilder.Build<BooksContext>(BooksContext.ConnectionName))
                    {
                        return db.GetAuthorsForPublisher(PublisherId);
                    }
                });

            _books = new Lazy<BookModel[]>(() =>
            {
                using (var db = ContextBuilder.Build<BooksContext>(BooksContext.ConnectionName))
                {
                    return db.GetBooksForPublisher(PublisherId);
                }
            });
        }

        public override AuthorModel[] Authors
        {
            get { return _authors.Value; }
        }

        public override BookModel[] Books
        {
            get { return _books.Value; }
        }

        private Lazy<AuthorModel[]> _authors;
        private Lazy<BookModel[]> _books;
    }
}